package com.sun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.dto.UserDTO;
import com.sun.entity.User;
import com.sun.mapper.SquadMapper;
import com.sun.mapper.UserMapper;
import com.sun.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SunRui
 * @since 2024-12-11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    SquadMapper squadMapper;
    @Override
    public List<User> selectUserBySquadId(Integer squadId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Squad_id",squadId);
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public int addUser(UserDTO userDTO) {
        User user = new User();
        user.setNickName(userDTO.getNickName());
        user.setGender(userDTO.getGender());
        user.setAge(userDTO.getAge());
        user.setSquadId(userDTO.getSquadId());
        // 新增的时候还要给小组人数 ＋1
        squadMapper.addMember(userDTO.getSquadId());
        return userMapper.insert(user);
    }

    @Override
    public int deleteUserById(Integer id,Integer squadId) {
        // 新增的时候还要给小组人数 -1
        squadMapper.deleteMember(squadId);
        QueryWrapper<User>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return userMapper.delete(queryWrapper);
    }
}
